package 二叉搜索数;

/**
 * https://leetcode.cn/problems/validate-binary-search-tree/description/?envType=study-plan-v2&envId=top-100-liked
 */
public class _98_验证二叉搜索树 {

    public boolean isValidBST(TreeNode root) {
        if (root == null) return true;
        return isValidBST(root, null, null);
    }

    private boolean isValidBST(TreeNode root, TreeNode min, TreeNode max) {
        if (root == null) return true;

        if (min != null && min.val >= root.val) return false;
        if (max != null && max.val <= root.val) return false;

        return isValidBST(root.left, min, root) && isValidBST(root.right, root, max);
    }
}
